<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>日志管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset" permission="sys:operationlog:query">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">IP地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="requestIp" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略IP地址筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日志描述</label>
                            <div class="layui-input-inline">
                                <input type="text" name="description" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略日志描述筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">操作人员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略操作人员筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">IP来源</label>
                            <div class="layui-input-inline">
                                <input type="text" name="address" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略IP来源筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日期范围</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="date-range" placeholder="不输入则忽略日期范围筛选"
                                       name="dateRange">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">请求耗时</label>
                            <div class="layui-input-inline">
                                <select name="timeRank">
                                    <option value="" selected>所有</option>
                                    <option value="1">10ms以内</option>
                                    <option value="2">10ms~1000ms</option>
                                    <option value="3">1000ms以上</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary"><i
                                    class="layui-icon layui-icon-fonts-clear"></i> 清 空
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"
                        permission="sys:operationlog:delete">批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"
               permission="sys:operationlog:delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/static/admin/layuimini/js/lay-module/layuimini/miniTab.js"></script>
<script src="/static/admin/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/static/admin/my/js/my.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'miniTab', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniTab = layui.miniTab,
            laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#date-range',
            range: '~',
            theme: 'molv'
        });
        table.render({
            elem: '#currentTableId',
            url: '/admin/operation-log',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50, fixed: "left"},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'requestIp', width: 150, title: 'IP', sort: true},
                    {field: 'address', width: 150, title: 'IP来源', sort: true},
                    {field: 'username', width: 150, title: '操作人员', sort: true},
                    {field: 'description', width: 150, title: '描述', sort: true},
                    {
                        field: 'status',
                        width: 150,
                        title: '访问状态',
                        align: 'center',
                        sort: true,
                        templet: function (data) {
                            let status = data.status;
                            let text = (status === 1 ? '成功' : '失败');
                            let color = (status === 1 ? 'green' : 'red');
                            let bgColor = 'layui-bg-' + color;
                            return '<span class="layui-badge ' + bgColor + '">' + text + '</span>';
                        }
                    },
                    {
                        field: 'time',
                        width: 150,
                        title: '请求耗时',
                        sort: true,
                        align: 'center',
                        templet: function (data) {
                            let time = data.time;
                            let color = time >= 1000 ? 'red' : (time >= 100 ? 'orange' : 'green');
                            let bgColor = 'layui-bg-' + color;
                            return '<span class="layui-badge ' + bgColor + '">' + time + 'ms</span>';
                        }
                    },
                    {field: 'browser', width: 150, title: '浏览器'},
                    {field: 'createTime', width: 220, title: '创建时间', sort: true},
                    {
                        title: '操作',
                        minWidth: 120,
                        toolbar: '#currentTableBar',
                        fixed: "right",
                        align: "center",
                        hide: (!hasPermission('sys:operationlog:delete'))
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function () {
                checkPermission();
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let requestIp = data.field.requestIp;
            let description = data.field.description;
            let username = data.field.username;
            let address = data.field.address;
            let dateRange = data.field.dateRange;
            let dates = dateRange.split(' ~ ')
            let startDate = dates[0];
            let endDate = dates[1];
            let timeRank = data.field.timeRank;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    requestIp: requestIp,
                    description: description,
                    username: username,
                    address: address,
                    startDate: startDate,
                    endDate: endDate,
                    timeRank: timeRank
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var idList = [];
                $.each(data, function () {
                    idList.push($(this)[0].id);
                });
                if (idList.length <= 0) {
                    return false;
                }
                Swal.fire({
                    title: '确定删除选中的' + idList.length + '条日志吗？',
                    text: '你将无法恢复它！',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: '确定删除！',
                    cancelButtonText: '取消删除！',
                    showLoaderOnConfirm: true,
                    preConfirm: () => {
                        return new Promise((resolve, reject) => {
                            axios({
                                method: 'delete',
                                url: '/admin/operation-log',
                                data: idList,
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest'
                                }
                            })
                                .then(response => {
                                    table.reload('currentTableId');
                                    resolve(response.data);
                                }, error => {
                                    reject(error);
                                })
                        }).then(res => {
                            Swal.fire({
                                icon: 'success',
                                title: '删除成功！',
                                showConfirmButton: false,
                                timer: 1500
                            });
                        }).catch(error => {
                            Swal.showValidationMessage(`删除失败: ${error.response.data.message}`);
                        })
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                Swal.fire({
                    title: '确定删除选中的这条日志吗？',
                    text: '你将无法恢复它！',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: '确定删除！',
                    cancelButtonText: '取消删除！',
                    showLoaderOnConfirm: true,
                    preConfirm: () => {
                        return new Promise((resolve, reject) => {
                            axios({
                                method: 'delete',
                                url: '/admin/operation-log/' + data.id,
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest'
                                }
                            })
                                .then(response => {
                                    obj.del();
                                    resolve(response.data);
                                }, error => {
                                    reject(error);
                                })
                        }).then(res => {
                            Swal.fire({
                                icon: 'success',
                                title: '删除成功！',
                                showConfirmButton: false,
                                timer: 1500
                            });
                        }).catch(error => {
                            Swal.showValidationMessage(`删除失败: ${error.response.data.message}`);
                        })
                    }
                });
            }
        });

    });
</script>

</body>
</html>